//
//  NSDate+iCT.m
//  iCareTek
//
//  Created by  Jvin_Zhuang on 14-7-21.
//  Copyright (c) 2014年 iCareTech. All rights reserved.
//

#import "NSDate+iCT.h"

#define DATE_COMPONENTS (NSYearCalendarUnit| NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit |  NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit | NSWeekdayOrdinalCalendarUnit | NSWeekOfYearCalendarUnit)

//#define DATE_COMPONENTS (NSCalendarUnitYear| NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekOfMonth |  NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal | NSCalendarUnitWeekOfYear)

#define CURRENT_CALENDAR [NSCalendar currentCalendar]

@implementation NSDate (iCT)

+ (NSInteger)convertToiCTWorkDay:(NSDate *)date
{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
//    NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
//    NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    NSDateComponents *comps;
    comps = [calendar components:DATE_COMPONENTS fromDate:date];
    NSInteger weekday = [comps weekday]; // 星期几（注意，此处周日是“1”，周一是“2”。。。。,需要做转换达到项目需求）
    if (weekday == 1)
    {
        weekday = 7;
    }
    else
        weekday -= 1;
    return weekday;
}

-(NSDate *)beSecond
{
    NSTimeInterval dt = self.timeIntervalSince1970;
    dt = floor(dt);
    return [NSDate dateWithTimeIntervalSince1970:dt];
}

-(NSDate *)beMinute
{
    NSTimeInterval dt = self.timeIntervalSince1970;
    dt = floor(dt)-(NSInteger)floor(dt)%60;
    return [NSDate dateWithTimeIntervalSince1970:dt];
}


//- (NSString *)weekString
//{
//    NSString *weekdaystring = @"";
//    switch (self.weekday) {
//        case 1:
//            weekdaystring = @"星期日";
//            break;
//        case 2:
//            weekdaystring = @"星期一";
//            break;
//        case 3:
//            weekdaystring = @"星期二";
//            break;
//        case 4:
//            weekdaystring = @"星期三";
//            break;
//        case 5:
//            weekdaystring = @"星期四";
//            break;
//        case 6:
//            weekdaystring = @"星期五";
//            break;
//        case 7:
//            weekdaystring = @"星期六";
//            break;
//            
//        default:
//            break;
//    }
//    
//    return NSLocalizedString(weekdaystring, @"周几");
//}

+ (NSDate *) startOfToday
{
    NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:[NSDate date]];
    components.hour = 0;
    components.minute = 0;
    components.second = 0;
    return [CURRENT_CALENDAR dateFromComponents:components];
}

+(NSDate *)now
{
    NSDate *date = [NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate: date];
    NSDate *localeDate = [date  dateByAddingTimeInterval: interval];
    NSLog(@"enddate=%@",localeDate);
    return localeDate;
}

+ (NSDate *)getDateFromDate:(NSDate *)date afterMonth:(NSInteger)month

{
    
    NSDateComponents *components = [[NSDateComponents alloc] init];
    
    components.month = month;
    
    NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
    NSDate *resultDate = [calender dateByAddingComponents:components toDate:date options:0];
    
    return resultDate;
    
}

@end
